package com.dragon.www.haier.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.activity.MainActivity;
import com.dragon.www.haier.database.DataHelper;
import com.dragon.www.haier.entity.Device;
import com.dragon.www.haier.entity.Room;
import com.dragon.www.haier.util.Utils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.Calendar;

/**
 * Created by daiye on 2016/4/11.
 */
public class RoomEditDialog extends Dialog {
    private Context context;
    private OnSelectListener listener;
    private Room room;
    private String mRoomImagePath = "";
    private Device device = new Device();

    private TextView tvAddPhoto;
    private Button btOK;
    private Button btCancel;
    private ImageView iv_add_photo;
    private EditText et_room_name;
    private EditText et_room_description;
    private EditText et_devices_name;
    private EditText et_devices_id;
    private EditText et_devices_ip;

    public RoomEditDialog(Context context) {
        super(context);
        this.context = context;
    }

    public RoomEditDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_room);
        tvAddPhoto = (TextView) findViewById(R.id.tv_photo_add);
        btOK = (Button) findViewById(R.id.bt_OK);
        btCancel = (Button) findViewById(R.id.bt_cancel);
        iv_add_photo = (ImageView) findViewById(R.id.iv_photo_add);
        et_room_name = (EditText) findViewById(R.id.et_room_name);
        et_room_description = (EditText) findViewById(R.id.et_room_description);
        et_devices_name = (EditText) findViewById(R.id.et_devices_name);
        et_devices_id = (EditText) findViewById(R.id.et_devices_id);
        et_devices_ip = (EditText) findViewById(R.id.et_devices_ip);

        mRoomImagePath = "";
        if (room != null) {
            mRoomImagePath = room.getImgPath();
            if(!TextUtils.isEmpty(mRoomImagePath)){
                ImageLoader.getInstance().displayImage(mRoomImagePath, iv_add_photo );
            }
            et_room_name.setText(room.getRoomName());
            et_room_description.setText(room.getRoomDesciption());
            device = DataHelper.getInstance().getDevice(room.getRoomid(), MainActivity.TYPE_CONTROLLER);
            if (device != null) {
                et_devices_name.setText(device.getDeviceName());
                et_devices_id.setText(Integer.toString(device.getDeviceId()));
                et_devices_ip.setText(device.getIp());
            }
        }

        tvAddPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.addPhoto();
                }
            }
        });

        iv_add_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.addPhoto();
                }
            }
        });

        btOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = et_room_name.getText().toString().trim();
                String description = et_room_description.getText().toString().trim();
                String devicesName = et_devices_name.getText().toString().trim();
                String devicesId = et_devices_id.getText().toString().trim();
                String devicesIp = et_devices_ip.getText().toString().trim();
                String devicesTemplateName= "";
                if(TextUtils.isEmpty(name)){
                    Toast.makeText(context, "请输入房间名称", Toast.LENGTH_SHORT).show();
                    Animation shake= AnimationUtils.loadAnimation(context, R.anim.shake);
                    et_room_name.startAnimation(shake);
                    return;
                }
//                if(TextUtils.isEmpty(devicesId)){
//                    Toast.makeText(context, "请输入id", Toast.LENGTH_SHORT).show();
//                    Animation shake= AnimationUtils.loadAnimation(context, R.anim.shake);
//                    et_devices_id.startAnimation(shake);
//                    return;
//                }
                if(TextUtils.isEmpty(devicesIp)){
                    Toast.makeText(context, "请输入ip", Toast.LENGTH_SHORT).show();
                    Animation shake= AnimationUtils.loadAnimation(context,R.anim.shake);
                    et_devices_ip.startAnimation(shake);
                    return;
                }else if(Utils.isboolIp(devicesIp)==false){
                    Toast.makeText(context, "ip格式错误："+devicesIp, Toast.LENGTH_SHORT).show();
                    Animation shake= AnimationUtils.loadAnimation(context,R.anim.shake);
                    et_devices_ip.startAnimation(shake);
                    return;
                }
                if (!TextUtils.isEmpty(name)) {
                    // 添加房间信息到数据库
                    // 新增
                    String msg;
                    if (room == null) {
                        msg = DataHelper.getInstance().addRoom(name, description, mRoomImagePath, Calendar.getInstance().getTimeInMillis(), devicesName, "1", devicesIp,devicesTemplateName);
                    }
                    // 修改
                    else {
                        msg = DataHelper.getInstance().updateRoom(room.getRoomid(), name, description);
                        DataHelper.getInstance().updateRoom(room.getRoomid(), mRoomImagePath);
                        if (device != null) {
                            DataHelper.getInstance().updateDevice(room.getRoomid(), device.getDeviceId(), devicesName, 1, MainActivity.TYPE_CONTROLLER, devicesIp, devicesTemplateName);
                        }

                    }
                    if (!TextUtils.isEmpty(msg)) {
                        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
                    }

                    if(listener!=null){
                        listener.ok();
                    }
                    dismiss();

                } else {
                    Toast.makeText(context, "名称不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
        btCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null){
                    listener.cancel();
                }
                dismiss();
            }
        });
    }

    @Override
    public void show() {
        if (context != null && ((Activity) context).isFinishing()) {
            return;
        }
        setCanceledOnTouchOutside(false);
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
    }

    public void setRoomImageAtButton(Uri uri){
        mRoomImagePath = uri.toString();
        if(!TextUtils.isEmpty(mRoomImagePath)){
            ImageLoader.getInstance().displayImage(mRoomImagePath, iv_add_photo );
        }
    }

    public void setRoom(Room room){
        this.room = room;
    }

    public void setLisntener(OnSelectListener listener) {
        this.listener = listener;
    }

    public static abstract interface OnSelectListener {
        public abstract void addPhoto();
        public abstract void ok();
        public abstract void cancel();
    }
}
